package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.yingshi.YingShiCameraDto;
import com.csun.cmny.provider.model.dto.yingshi.YingShiCameraQueryDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.GroupVo;
import com.csun.cmny.provider.model.vo.yingshi.GroupCameraTreeVo;
import com.csun.cmny.provider.model.vo.yingshi.YingShiCameraVo;
import com.csun.cmny.provider.model.vo.yingshi.YsCameraChannelVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcYingShiCameraService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/8/27 16:37
 * @Description: 萤石摄像头控制器
 */
@RestController
@RequestMapping(value = "/yingShiCamera", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcYingShiCameraController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcYingShiCameraService dmcYingShiCameraService;
    @Resource
    private UacRpcService uacRpcService;

    /**
     * 查询萤石摄像头列表 - 分页
     *
     * @param yingShiCameraQueryDto the ying shi camera query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody YingShiCameraQueryDto yingShiCameraQueryDto) {

        yingShiCameraQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        yingShiCameraQueryDto.setDeviceType(DmcDeviceTypeEnum.YS_CAMERA.getIndex());

        List<YingShiCameraVo> yingShiCameraVoList = dmcYingShiCameraService.queryListWithPage(yingShiCameraQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(yingShiCameraVoList));
    }

    /**
     * 添加萤石摄像头信息
     *
     * @param yingShiCameraDto  the ying shi camera dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@Valid @RequestBody YingShiCameraDto yingShiCameraDto) {

        Integer result = dmcYingShiCameraService.save(yingShiCameraDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 修改萤石摄像头信息
     *
     * @param yingShiCameraDto  the ying shi camera dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody YingShiCameraDto yingShiCameraDto) {

        Integer result = dmcYingShiCameraService.edit(yingShiCameraDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 根据id删除萤石摄像头信息
     *
     * @param id    the id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcYingShiCameraService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 查询集团化 - 摄像头树
     *
     * @return
     */
    @RequestMapping(value = "/getGroupTree", method = RequestMethod.POST)
    public Wrapper getGroupTree() {

        Long groupId = getLoginAuthDto().getGroupId();
        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);

        List<GroupCameraTreeVo> groupCameraTreeVoList = Lists.newArrayList();
        if (PublicUtil.isNotEmpty(groupVoList)) {
            // 根据设备机构id获取摄像头列表
            groupCameraTreeVoList = dmcYingShiCameraService.queryGroupCameraList(groupVoList, groupId);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), groupCameraTreeVoList);
    }

    /**
     * 查询集团化 - 摄像头通道列表
     *
     * @return
     */
    @RequestMapping(value = "/getChannelList", method = RequestMethod.POST)
    public Wrapper getChannelList(@RequestBody YingShiCameraQueryDto yingShiCameraQueryDto) {

        Long groupId = getLoginAuthDto().getGroupId();
        if (PublicUtil.isNotEmpty(yingShiCameraQueryDto.getGroupId())) {
            groupId = yingShiCameraQueryDto.getGroupId();
        }
        yingShiCameraQueryDto.setGroupId(null);

        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);

        List<YsCameraChannelVo> ysCameraChannelVoList = Lists.newArrayList();
        if (PublicUtil.isNotEmpty(groupVoList)) {

            List<Long> groupIdList = Lists.newArrayList();
            for (GroupVo gv : groupVoList) {
                groupIdList.add(gv.getId());
            }
            yingShiCameraQueryDto.setGroupIdList(groupIdList);
            ysCameraChannelVoList = dmcYingShiCameraService.getChannelList(yingShiCameraQueryDto);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(ysCameraChannelVoList));
    }
}